<template>
	<view class="container">
		<u-navbar title="修改地址" :is-fixed="true"></u-navbar>
		<!-- <u-form></u-form> -->
		<form @submit="saveData" report-submit="true">
			<view class="address-cont-title b-f b-b">
				<view class="list address-box dis-flex b-b">
					<view class="left-name"><text class="f-30">收货人</text></view>
					<view class="right-cont flex-box"><input name="name" placeholder="请输入收货人姓名" v-model="detail.name" /></view>
				</view>
				<view class="list address-box dis-flex">
					<view class="left-name"><text class="f-30">手机号</text></view>
					<view class="right-cont flex-box"><input name="phone" placeholder="请输入收货人联系电话" type="number" v-model="detail.phone" /></view>
				</view>
			</view>
			<view class="address-cont-title b-f b-b">
				<view class="dis-flex list address-box m-top20 b-b">
					<view class="left-name"><text class="f-30">所在地区</text></view>
					<view class="address-cont-box flex-box" @tap.stop="regionShow = true">
						<text v-if="region" class="f-30">{{ region }}</text>
						<text v-else class="f-30 col-7">选择省、市、区</text>
					</view>
				</view>
				<view class="list address-box dis-flex">
					<view class="left-name"><text class="f-30">详细地址</text></view>
					<view class="right-cont flex-box"><input name="detail" placeholder="请输入街道小区楼层" type="text" v-model="detail.detail" /></view>
				</view>
			</view>
			<view class="padding-box m-top35 profile-btn"><button formType="submit" :disabled="disabled">保存</button></view>
		</form>
		<u-select v-model="regionShow" mode="mutil-column-auto" :list="regionList" @confirm="confirmRegion"></u-select>
	</view>
</template>

<script>
import Tools from '@/utils/Tools.js';
export default {
	data() {
		return {
			disabled: false,
			nav_select: false, // 快捷导航
			region: '',
			detail: {},
			regionData: [],
			error: '',
			regionShow: false,
			regionList: []
		};
	},
	onLoad(options) {
		this.$u.api.getRegionList().then(res => {
			this.regionList = res.data.data;
		});
		this.getAddressDetail(options.address_id);
		// const regionPickerData = Tools.regionHandle(regionData.data);
		// this.regionValue = regionPickerData.regionValue;
		// this.regionRange = regionPickerData.regionRange;
		// if (Tools.isWechat()) {
		// 	this.isWechat = true;
		// 	this.$wechat.initJssdk(() => {
		// 		this.jssdkReady = true;
		// 	});
		// }
	},
	methods: {
		/**
		 * 获取当前地址信息
		 */
		getAddressDetail: function(address_id) {
			this.$u.api.getAddressDetail(address_id).then(res => {
				this.detail = res.data.detail;
				this.region = res.data.region.join(' ');
			});
		},
		/**
		 * 表单提交
		 */
		saveData: function(e) {
			let values = e.detail.value;
			values.region = this.region.split(' ').join(',');

			// 表单验证
			if (!this.validation(values)) {
				Tools.showError(this.error);
				return false;
			}

			// 按钮禁用
			this.disabled = true;

			// 提交到后端
			values.address_id = this.detail.address_id;
			this.$u.api
				.editAddress(values)
				.then(res => {
					this.$u.toast('保存成功');
					setTimeout(() => {
						this.navigateBack();
					}, 1500);
				})
				.catch(res => {
					this.disabled = false;
				});
		},
		/**
		 * 表单验证
		 */
		validation(values) {
			if (values.name === '') {
				this.error = '收件人不能为空';
				return false;
			}
			if (!this.$u.test.mobile(values.phone)) {
				this.error = '手机号不符合要求';
				return false;
			}
			if (!this.region) {
				this.error = '省市区不能空';
				return false;
			}
			if (values.detail === '') {
				this.error = '详细地址不能为空';
				return false;
			}
			return true;
		},
		confirmRegion(e) {
			const regionList = [];
			for (let i = 0; i < e.length; i++) {
				regionList.push(e[i].label);
			}
			this.region = regionList.join(' ');
		}
	}
};
</script>

<style>
.profile-list .admin {
	padding-left: 15px;
	font-size: 30rpx;
	color: #333;
}

.address-box .left-name {
	width: 85px;
}

.address-cont-title .list {
	width: auto;
	padding: 24rpx;
}

.tui-picker-detail text {
	padding: 0 10rpx;
}

.infoText {
	line-height: 56rpx;
	display: block;
}

picker-view {
	background-color: white;
	padding: 0;
	width: 100%;
	height: 480rpx;
	bottom: 0;
	position: fixed;
}

picker-view-column view {
	vertical-align: middle;
	font-size: 28rpx;
	line-height: 28rpx;
	height: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
}

.animation-element-wrapper {
	display: flex;
	position: fixed;
	left: 0;
	top: 0;
	height: 100%;
	width: 100%;
	z-index: 21;
}

.animation-element {
	display: flex;
	position: fixed;
	width: 100%;
	height: 570rpx;
	bottom: 0;
	background-color: rgba(255, 255, 255, 1);
}

.animation-button {
	margin-top: 20rpx;
	top: 20rpx;
	width: 400rpx;
	height: 100rpx;
	line-height: 100rpx;
	align-items: center;
}

.address-text {
	color: #999;
	display: inline-flex;
	position: fixed;
	margin-top: 20rpx;
	height: 50rpx;
	text-align: center;
	line-height: 50rpx;
	font-size: 30rpx;
	font-family: Arial, Helvetica, sans-serif;
}

.left-bt {
	left: 30rpx;
}

.right-bt {
	right: 30rpx;
	color: #ec5151;
}

.line {
	display: block;
	position: fixed;
	height: 1rpx;
	width: 100%;
	margin-top: 89rpx;
	background-color: #eee;
}

.address-box .right-cont input {
	width: 100%;
	font-size: 30rpx;
	color: #444;
	height: 52rpx;
}

.bargain-commont-bg {
	background: rgba(0, 0, 0, 0.6);
	position: fixed;
	right: 0;
	left: 0;
	top: 0;
	bottom: 0;
	z-index: 20;
}

.f-34 {
	font-size: 34rpx;
}

.f-32 {
	font-size: 32rpx;
}

.f-31 {
	font-size: 31rpx;
}

.f-28 {
	font-size: 28rpx;
}

.f-26 {
	font-size: 26rpx;
}

.f-24 {
	font-size: 24rpx;
}

.f-22 {
	font-size: 22rpx;
}

.b-r {
	border-right: 1rpx solid #eee;
}

.b-b {
	border-bottom: 1rpx solid #eee;
}

.b-t {
	border-top: 1rpx solid #eee;
}

.m-top4 {
	margin-top: 4rpx;
}

.m-top10 {
	margin-top: 10rpx;
}

.m-top20 {
	margin-top: 25rpx;
}

.m-top35 {
	margin-top: 35rpx;
}
</style>
